package vashion.azeroth.biz.impl;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vashion.azeroth.activity.manager.GlobalActivityInfoManager;
import vashion.azeroth.biz.GoodItemService;
import vashion.azeroth.contant.ForestErrorEnum;
import vashion.azeroth.core.activity.ActiveSchemaEnum;
import vashion.azeroth.core.activity.GlobalActivityExtendInfoDO;
import vashion.azeroth.core.biz.BannerVO;
import vashion.azeroth.core.biz.BrandVO;
import vashion.azeroth.core.biz.GoodItemVO;
import vashion.azeroth.core.item.pojo.ForestDO;
import vashion.azeroth.core.item.pojo.ItemDO;
import vashion.azeroth.core.item.pojo.PropertyValueDO;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.item.BrandService;
import vashion.azeroth.core.service.item.ForestService;

import java.util.List;
import java.util.Map;

/**
 * Created by liuhe on 15/10/12.
 */
@Service
public class GoodItemServiceImpl implements GoodItemService {

    final static Logger logger = LoggerFactory.getLogger(GoodItemServiceImpl.class);

    @Autowired
    private ForestService forestService;
    @Autowired
    private BrandService brandService;

    @Autowired
    private GlobalActivityInfoManager globalActivityManager;

    public ApiResult<GoodItemVO> getGoodsInfo(boolean needBanner, int pageNO, int pageSize) {
        ApiResult<GoodItemVO> result = new ApiResult<GoodItemVO>();
        GoodItemVO goodItemVO = new GoodItemVO();
        List<GlobalActivityExtendInfoDO> bannerExtendInfoDOList = Lists.newArrayList();
        if (needBanner) {
            bannerExtendInfoDOList = globalActivityManager.getCurrentActivityExtendList(ActiveSchemaEnum.YANGTAO_INDEX_BANNER_TYPE.getActivityType());
        }

        pageNO = pageNO <= 1 ? 1 : pageNO;
        GlobalActivityExtendInfoDO extendInfoDO = new GlobalActivityExtendInfoDO();
        extendInfoDO.setActivityType(ActiveSchemaEnum.NEW_YANGTAO_INDEX_JINGXUAN.getActivityType());
        extendInfoDO.setFirstRow((pageNO - 1) * pageSize);
        extendInfoDO.setPageSize(pageSize);
        List<GlobalActivityExtendInfoDO> goodItemExtendInfoDOList = globalActivityManager.getCurrentActivityExtendList(extendInfoDO);

        goodItemVO.setTotalCount(goodItemExtendInfoDOList.size());
        goodItemVO.setBannerVOList(buildBanner(bannerExtendInfoDOList));
        goodItemVO.setItemDOList(buildGoodItem(goodItemExtendInfoDOList));
        result.setData(goodItemVO);
        result.setSuccess(true);
        return result;
    }

    private List<ItemDO> buildGoodItem(List<GlobalActivityExtendInfoDO> goodItemExtendInfoDOList) {
        List<ItemDO> resultList = Lists.newArrayList();
        for (GlobalActivityExtendInfoDO globalActivityExtendInfoDO : goodItemExtendInfoDOList) {
            ItemDO itemDO = ItemDO.buildByActivity(globalActivityExtendInfoDO);
            if (null == itemDO) {
                logger.error("itemDO build error, globalActivityExtendInfoDO=" + globalActivityExtendInfoDO);
                continue;
            }
            resultList.add(itemDO);
        }
        return resultList;
    }

    private List<BannerVO> buildBanner(List<GlobalActivityExtendInfoDO> bannerExtendInfoDOList) {

        List<BannerVO> resultList = Lists.newArrayList();
        for (GlobalActivityExtendInfoDO globalActivityExtendInfoDO : bannerExtendInfoDOList) {
            BannerVO bannerVO = BannerVO.buildByActivity(globalActivityExtendInfoDO);
            if (null == bannerVO) {
                logger.error("bannerVO build error, globalActivityExtendInfoDO=" + globalActivityExtendInfoDO);
                continue;
            }
            resultList.add(bannerVO);
        }
        return resultList;
    }

    public ApiResult<List<ForestDO>> getChildForestList() {
        ApiResult<Map<ForestDO, List<ForestDO>>> forestTree = forestService.getForestTree();
        if (forestTree.isSuccess()) {
            List<ForestDO> childForestDOList = Lists.newArrayList();
            for (List<ForestDO> forestDOList : forestTree.getData().values()) {
                childForestDOList.addAll(forestDOList);
            }
            return ApiResult.newSuccessResult(childForestDOList);
        }
        return ApiResult.newErrorResult(ForestErrorEnum.SYSTEM_ERROR.getErrorCode(), ForestErrorEnum.SYSTEM_ERROR.getErrorMessage());
    }

    public ApiResult<List<BrandVO>> getBrandList() {
        ApiResult<List<PropertyValueDO>> brandListResult = brandService.getBrandList(null, null);
        if (!brandListResult.isSuccess()) {
            return ApiResult.newErrorResult(brandListResult.getErrorCode(), brandListResult.getErrorMsg());
        }
        ApiResult<List<BrandVO>> result = new ApiResult<List<BrandVO>>();
        result.setSuccess(true);
        List<BrandVO> voList = Lists.newArrayList();
        for (PropertyValueDO propertyValueDO : brandListResult.getData()) {
            BrandVO brandVO = new BrandVO();
            brandVO.setId(propertyValueDO.getId());
            brandVO.setTitle(propertyValueDO.getPropertyMetaTitle());
            brandVO.setImage(propertyValueDO.getFeatureImage());
            voList.add(brandVO);
        }
        result.setData(voList);
        return result;
    }
}
